/*
 * (C) 2014-2015 Alibaba Group Holding Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *
 *     __  ___   ______        ___       __          ___
 *    |  |/  /  /  __  \      /   \     |  |        /   \
 *    |  '  /  |  |  |  |    /  ^  \    |  |       /  ^  \
 *    |    <   |  |  |  |   /  /_\  \   |  |      /  /_\  \
 *    |  .  \  |  `--'  |  /  _____  \  |  `----./  _____  \
 *    |__|\__\  \______/  /__/     \__\ |_______/__/     \__\
 *
 *
 *
 */
//
// Created by KOALA TEAM on 12/19/14.
//

#import "KLObjcUtils.h"
#import <objc/runtime.h>
#import <objc/objc.h>


NSArray * getPropertyNameList(Class objClass)
{
    NSMutableArray * propertyNames = [NSMutableArray array];
    unsigned int propertyCount = 0;
    objc_property_t * properties = class_copyPropertyList(objClass, &propertyCount);

    for (unsigned int i = 0; i < propertyCount; ++i) {
        objc_property_t property = properties[i];
        const char * name = property_getName(property);
        [propertyNames addObject:[NSString stringWithUTF8String:name]];
        
    }
    
    if(properties){
        free(properties);
        properties = NULL;
    }
 
    
    return propertyNames;
}


NSArray * getAllPropertyNameList(Class objClass)
{
    NSMutableArray * propertyNames = [NSMutableArray array];
    Class superClass = [objClass superclass];
    while( superClass != objClass && objClass){
        unsigned int propertyCount = 0;
        objc_property_t * properties = class_copyPropertyList(objClass, &propertyCount);
        
        for (unsigned int i = 0; i < propertyCount; ++i) {
            objc_property_t property = properties[i];
            const char * name = property_getName(property);
            [propertyNames addObject:[NSString stringWithUTF8String:name]];
            
        }
        if(properties){
            free(properties);
            properties = NULL;
        }
        objClass = superClass;
        superClass = [objClass superclass];
    }
    
    
    return [propertyNames copy];
}

NSArray * getPropertyNameListEx(Class object,Class endClass)
{
    if(endClass == nil){
        return getAllPropertyNameList(object);
    }else{
        NSMutableArray * propertyNames = [NSMutableArray array];
        Class superClass = [object superclass];
        Class objClass = [object class];
        while(objClass && objClass != endClass && superClass != objClass){
            unsigned int propertyCount = 0;
            objc_property_t * properties = class_copyPropertyList(objClass, &propertyCount);
            
            for (unsigned int i = 0; i < propertyCount; ++i) {
                objc_property_t property = properties[i];
                const char * name = property_getName(property);
                if(name)
                    [propertyNames addObject:[NSString stringWithUTF8String:name]];
            }
            
            if(properties){
                free(properties);
                properties = NULL;
            }
            objClass = superClass;
            superClass = [objClass superclass];
        }
        return [propertyNames copy];
    }
}

